package com.leyou.item.controller;

import com.leyou.item.interfaces.pojo.SpecGroup;
import com.leyou.item.interfaces.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规格参数的相关信息
 * @Author yong.Mr
 * @data 2019-8-9
 * @version 1.0.0
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/spec")
public class SpecificationController {

    /**
     * 注入SpecificationService
     */
    @Autowired
    private SpecificationService specificationService;

    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> groups = this.specificationService.queryGroupsByCid(cid);
        if(CollectionUtils.isEmpty(groups)){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }

    /**
     * 根据条查询规格参数
     * @param cid 商品分类id
     * @param gid 规格组id
     * @param generic 是否是sku通用属性
     * @param searching 是否用于搜索过滤
     * @return ResponseEntity<List<SpecParam>>
     */
    @RequestMapping("/params")
    public ResponseEntity<List<SpecParam>> queryParams(
           @RequestParam(value = "cid",required = false) Long cid,
           @RequestParam(value = "gid",required = false) Long gid,
           @RequestParam(value = "generic",required = false) Boolean generic,
           @RequestParam(value = "searching",required = false) Boolean searching
    ){
        System.out.println("数据是否过来了：" + cid+ " " + gid + " " + generic);
        List<SpecParam> specParams = specificationService.queryParam(cid, gid, generic, searching);
        // 判断吗是否查询成功
        if(CollectionUtils.isEmpty(specParams)){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParams);
    }

    @GetMapping("group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsWithParam(@PathVariable("cid") Long cid) {
        List<SpecGroup> groups = this.specificationService.queryGroupsWithParam(cid);
        // 判断吗是否查询成功
        if(CollectionUtils.isEmpty(groups)){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }

}
